package cn.lookup.controller;

import cn.lookup.domain.User;
import cn.lookup.domain.VO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/success", params = {"user"}, method = RequestMethod.GET)
    public String success() {
        System.out.println("UserController success is running...");
        return "success";
    }

    @RequestMapping(value = "/mav")
    public ModelAndView mav() {
        System.out.println("UserController mav is running...");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username", "test");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/mav2")
    public ModelAndView mav2(ModelAndView modelAndView) {
        System.out.println("UserController mav2 is running...");
        modelAndView.addObject("username", "test mav2");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/mav3")
    public String mav3(Model model) {
        System.out.println("UserController mav3 is running...");
        model.addAttribute("username", "mav3");
        return "success";
    }

    @RequestMapping(value = "/mav4")
    public String mav4(HttpServletRequest request) {
        System.out.println("UserController mav4 is running...");
        request.setAttribute("username", "mav4");
        return "success";
    }

    @RequestMapping(value = "/mav5")
    public void mav5(HttpServletResponse response) {
        System.out.println("UserController mav5 is running...");
        try {
            response.getWriter().println("132");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/mav6")
    @ResponseBody
    public String mav6() {
        System.out.println("UserController mav6 is running...");
        return "123123123";
    }

    @RequestMapping(value = "/mav7")
    @ResponseBody
    public String mav7() {
        System.out.println("UserController mav7 is running...");
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }

    @RequestMapping(value = "/mav8")
    @ResponseBody
    public String mav8() {
        System.out.println("UserController mav8 is running...");
        User zs = new User("zhangsan", 18);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = null;
        try {
            json = objectMapper.writeValueAsString(zs);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    @RequestMapping(value = "/mav9")
    @ResponseBody
    //配置处理器适配器  自动将数据通过jackson转json返回
    public User mav9() {
        System.out.println("UserController mav9 is running...");
        User zs = new User("jackson", 18);
        return zs;
    }

    @RequestMapping(value = "/mav10")
    @ResponseBody
    public void mav10(String name, String age) {
        System.out.println(name);
        System.out.println(age);
    }

    @RequestMapping(value = "/mav11")
    @ResponseBody
    public void mav11(User user) {
        System.out.println(user);
    }

    @RequestMapping(value = "/mav12")
    @ResponseBody
    public void mav12(String[] names) {
        System.out.println(Arrays.toString(names));
    }

    @RequestMapping(value = "/mav13")
    @ResponseBody
    public void mav13(VO vo) {
        System.out.println(vo);
    }

    @RequestMapping(value = "/mav14")
    @ResponseBody
    public void mav14(@RequestBody ArrayList<User> list) {
        System.out.println(list);
    }

    @RequestMapping(value = "/mav15")
    @ResponseBody
    public void mav15(@RequestParam(value = "username", required = false, defaultValue = "defaultName") String name) {
        System.out.println(name);
    }

    @RequestMapping("/mav16/{username}")
    @ResponseBody
    public void mav16(@PathVariable(value = "username", required = false) String name) {
        System.out.println(name);
    }

    @RequestMapping("/mav17")
    @ResponseBody
    public void mav17(@RequestParam("date") Date date) {
        System.out.println(date);
    }

    @RequestMapping("/mav18")
    @ResponseBody
    public void mav18(@RequestHeader("User-Agent") String userAgent) {
        System.out.println(userAgent);
    }

    @RequestMapping("/mav18/1")
    @ResponseBody
    public void mav181(@RequestHeader("Cookie") String cookie) {
        System.out.println(cookie);
    }

    @RequestMapping("/mav19")
    @ResponseBody
    public void mav19(@CookieValue("JSESSIONID") String JSESSIONID) {
        System.out.println(JSESSIONID);
    }


}
